﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using NRadioTray.App.Domain;

namespace NRadioTray.App.Options
{
    /// <summary>
    /// Class <see cref="OptionsView"/>.
    /// </summary>
    public partial class OptionsView : Form, IOptionsView
    {
        /// <summary>
        /// 
        /// </summary>
        private IOptionViewObserver observer;

        /// <summary>
        /// Gets the selected item.
        /// </summary>
        private Radio SelectedItem
        {
            get
            {
                if (this.listView1.SelectedItems == null
                    ||  this.listView1.SelectedItems.Count != 1
                    || !(this.listView1.SelectedItems[0].Tag is Radio))
                {
                    return null;
                }

                return this.listView1.SelectedItems[0].Tag as Radio;
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="OptionsView"/> class.
        /// </summary>
        public OptionsView()
        {
            InitializeComponent();

            this.Icon = Properties.Resources.nradiotray_ico;
        }

        /// <summary>
        /// Sets the observer.
        /// </summary>
        /// <param name="observer">The observer.</param>
        public void SetObserver(IOptionViewObserver observer)
        {
            this.observer = observer;
        }

        public Radio SelectedRadio { get; set; }

        public void ShowRadios(IList<Domain.Radio> radios)
        {
            if (radios == null)
            {
                return;
            }

            this.listView1.BeginUpdate();
            this.listView1.Items.Clear();

            foreach (var radio in radios)
            {
                ListViewItem item = new ListViewItem(radio.Name);
                item.Tag = radio;

                this.listView1.Items.Add(item);

            }

            this.listView1.EndUpdate();
        }

        private void ButtonAddClick(object sender, EventArgs e)
        {
            this.observer.AddRadio();
        }

        /// <summary>
        /// Buttons the edit click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void ButtonEditClick(object sender, EventArgs e)
        {
            if (this.SelectedItem == null)
            {
                return;
            }
            
            this.observer.EditRadio(SelectedItem);
        }

        /// <summary>
        /// Buttons the remove click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void ButtonRemoveClick(object sender, EventArgs e)
        {
            if (this.SelectedItem == null)
            {
                return;
            }

            var result = MessageBox.Show(this,
                string.Format(
                    "Remove radio: {0}. \r\nAre you sure?",
                    this.SelectedItem.Name),
                    "Remove radio",
                MessageBoxButtons.YesNo);

            if (result == System.Windows.Forms.DialogResult.Yes)
            {
                this.observer.RemoveRadio(this.SelectedItem);
            }
        }

        /// <summary>
        /// Buttons the move up click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void ButtonMoveUpClick(object sender, EventArgs e)
        {
            if (this.SelectedItem == null)
            {
                return;
            }

            this.observer.MoveUp(this.SelectedItem);
        }

        /// <summary>
        /// Buttons the move down click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void ButtonMoveDownClick(object sender, EventArgs e)
        {
            if (this.SelectedItem == null)
            {
                return;
            }

            this.observer.MoveDown(this.SelectedItem);
        }
    }
}
